package cn.com.bluemoon.daps.model.utils.email;

import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.model.entity.DapDataModel;
import cn.com.bluemoon.daps.model.entity.DapDataModelRecord;
import cn.com.bluemoon.daps.notice.template.ApplyPublishMessage;
import cn.com.bluemoon.daps.notice.template.base.BaseMessage;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasource;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author karry
 * @Data 2021/7/28
 * @Time:16:07
 */

public class EmailUtils {

    public static final Map<Integer, String> BUSINESS_LINE = new HashMap() {
        {
            put(1, "现代渠道");
            put(2, "电商");
            put(3, "财务");
            put(4, "供应链");
            put(5, "人事行政后勤");
            put(6, "社区（含高校等）");
            put(7, "洗衣服务");
            put(8, "月亮商城");
            put(9, "客服");
            put(10, "管理学院");
        }
    };

    private static final String URL = " 请登录数据资产平台执行下一步操作：http://domp.bluemoon.com.cn/bd-portal-view/index.html#/index";

    private static final String MESSAGE = "本邮件仅通知，无需人工处理。";

    private static final String APPLY_PUBLIC = "申请模型发布";

    private static final String VERIFY = "模型发布审核结果";

    private static final String EMAIL_CATEGORY_UN_VERIFY = "模型管理-模型发布：待审核通知-系统消息";

    private static final String EMAIL_CATEGORY_PASS = "模型管理-模型发布：审核通过通知-系统消息";

    private static final String EMAIL_CATEGORY_UN_PASS = "模型管理-模型发布：审核不通过通知-系统消息";
    /**
     *
     * @param autoPass 1 通过、2.不通过、3未开启
     * @param dapDataModel
     * @param dropSql
     * @param datasource
     * @param manAuto 1,2,3,4代表通过、不需要、待审核、不通过
     * @return
     */
    public static ApplyPublishMessage sendToUserMessage(Integer autoPass, DapDataModel dapDataModel, List<String> dropSql,
                                                        DapSystemDatasource datasource, DapDataModelRecord record, Integer manAuto) {

        ApplyPublishMessage message = new ApplyPublishMessage();
        String header = manAuto == 3 ? "待审核" : manAuto == 4 ? "审核不通过" : "审核通过";
        message.setHeader(String.format(" 您申请的模型发布%s", header));
        message.setModelName(dapDataModel.getName());
        message.setBizDesc(BUSINESS_LINE.get(dapDataModel.getBusinessLine()));
        message.setChangeOrder(record.getChangeOrder());
        message.setChangeDesc(record.getChangeDesc());
        String format = dropSql == null || dropSql.isEmpty() ? "否" : "是";
        message.setDangerStats(format);
        message.setAutoAudit(autoPass == 1 ? "通过" : autoPass == 2 ? "不通过" : "未开启");
        message.setManAudit(manAuto == 1 ? "通过" : manAuto == 2 ? "不需要" : manAuto == 3 ? "待审核" : "不通过");
        message.setDataSourceName(datasource.getName());
        message.setFooter(manAuto == 1 || manAuto == 4 ? MESSAGE : URL);
        message.setAction(manAuto == 1 && manAuto == 4 ? VERIFY : APPLY_PUBLIC);
        message.setTopic(manAuto == 3 ? EMAIL_CATEGORY_UN_VERIFY : manAuto == 4 ? EMAIL_CATEGORY_UN_PASS : EMAIL_CATEGORY_PASS);
        message.setSyncStatus(BaseMessage.SyncStatus.Success);
        String account = UserInfoHolder.getAccount();
        // 邮件发送人
        message.setSender("数据资产平台");
        // 当前模型管理员
        String modelAdmin = dapDataModel.getModelAdmin();
        int length = 8 - modelAdmin.length();
        if(length > 0){
            for (int i = 0; i< length; i++){
                modelAdmin =  "0" + modelAdmin;
            }
        }
        String emailDress = String.format("%s@bluemoon.com.cn", modelAdmin);
        List<String> userList = Arrays.asList(emailDress);
        // 邮件接收人
        message.setRcs(userList);
        return message;
    }


    /**
     *
     * @param autoPass 1 通过、2.不通过、3未开启
     * @param account
     * @param dapDataModel
     * @param dropSql
     * @param datasource
     * @param manAuto 1,2,3,4代表通过、不需要、待审核、不通过
     * @return
     */
    public static ApplyPublishMessage sendToDbaMessage(Integer autoPass, String account, DapDataModel dapDataModel,
                                                        List<String> dropSql, DapSystemDatasource datasource,
                                                       DapDataModelRecord record, Integer manAuto, List<String> dba) {

        ApplyPublishMessage message = new ApplyPublishMessage();
        String header = manAuto == 2 ? "审核通过" : "待审核";
        message.setHeader(String.format("用户%s申请模型发布%s", account, header));
        message.setModelName(dapDataModel.getName());
        message.setBizDesc(BUSINESS_LINE.get(dapDataModel.getBusinessLine()));
        message.setChangeOrder(record.getChangeOrder());
        message.setChangeDesc(record.getChangeDesc());
        String format = dropSql.isEmpty() ? "否" : "是";
        message.setDangerStats(format);
        message.setAutoAudit(autoPass == 1 ? "通过" : autoPass == 2 ? "不通过" : "未开启");
        message.setManAudit(manAuto == 2 ? "不需要" :"待审核");
        message.setDataSourceName(datasource.getName());
        message.setFooter(manAuto == 2 ? MESSAGE : URL);
        message.setTopic(manAuto == 3 ? EMAIL_CATEGORY_UN_VERIFY : EMAIL_CATEGORY_PASS);
        message.setSyncStatus(BaseMessage.SyncStatus.Success);
        // 邮件发送人
        message.setSender("数据资产平台");
        // 邮件接收人
        message.setRcs(dba);
        return message;
    }
}
